package net.zjitc.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.fasterxml.jackson.databind.ser.Serializers;
import lombok.extern.slf4j.Slf4j;
import net.zjitc.common.BaseContext;
import net.zjitc.common.R;
import net.zjitc.entiy.ShoppingCart;
import net.zjitc.service.ShoppingCartService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.List;

@Slf4j
@RestController
@RequestMapping("/shoppingCart")
public class ShoppingCartController {
    @Autowired
    private ShoppingCartService shoppingCartService;
    //查询购物车
    //请求 URL: http://localhost:8080/shoppingCart/list
    //请求方法: GET
    @GetMapping("/list")
    public R<List<ShoppingCart>> list(){
        log.info("查看购物⻋...");

        LambdaQueryWrapper<ShoppingCart> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(ShoppingCart::getUserId, BaseContext.getCurrentId());
        queryWrapper.orderByAsc(ShoppingCart::getCreateTime);

        List<ShoppingCart> list = shoppingCartService.list(queryWrapper);
        return R.success(list);
    }
    //添加购物车
    //请求 URL: http://localhost:8080/shoppingCart/add
    //请求方法: POST
    @PostMapping("/add")
    public R<ShoppingCart> add(@RequestBody ShoppingCart shoppingCart){
        log.info("购物车数据:{}",shoppingCart);
        //设置用户id，指定当前是哪个用户的购物车数据
        Long currentId=BaseContext.getCurrentId();
        shoppingCart.setUserId(currentId);
        Long dishId=shoppingCart.getDishId();
        LambdaQueryWrapper<ShoppingCart> queryWrapper=new LambdaQueryWrapper<>();
        queryWrapper.eq(ShoppingCart::getUserId,currentId);
        if (dishId!=null){
            //添加到购物车的是菜品
            queryWrapper.eq(ShoppingCart::getDishId,dishId);
        }else {
            //添加到购物车的是套餐
            queryWrapper.eq(ShoppingCart::getSetmealId,shoppingCart.getSetmealId());
        }
        //查询当前菜品是否在购物车中
        ShoppingCart cartServiceOne=shoppingCartService.getOne(queryWrapper);
        if (cartServiceOne !=null){
            //如果存在，就在原来数量上加一
            Integer number=cartServiceOne.getNumber();
            cartServiceOne.setNumber(number+1);
            shoppingCartService.updateById(cartServiceOne);
        }else {
            //如果不存在，则添加到购物车
            shoppingCart.setNumber(1);
            shoppingCart.setCreateTime(LocalDateTime.now());
            shoppingCartService.save(shoppingCart);
            cartServiceOne=shoppingCart;
        }
        return R.success(cartServiceOne);
    }
    //清空购物车
    //请求 URL: http://localhost:8080/shoppingCart/clean
    //请求方法: DELETE
    @DeleteMapping("/clean")
    public R<String> clean(){
        LambdaQueryWrapper<ShoppingCart> queryWrapper=new LambdaQueryWrapper<>();
        queryWrapper.eq(ShoppingCart::getUserId,BaseContext.getCurrentId());
        shoppingCartService.remove(queryWrapper);
        return R.success("清空购物车成功");
    }
    //减少
    //请求 URL: http://localhost:8080/shoppingCart/sub
    //请求方法: POST
    @PostMapping("/sub")
    public R<ShoppingCart> sub(@RequestBody ShoppingCart shoppingCart) {
        log.info("sub shopping cart" + shoppingCart);
        //如果这个条数据为1
        Long dishId=shoppingCart.getDishId();
        LambdaQueryWrapper<ShoppingCart> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        if (dishId!=null){
            lambdaQueryWrapper.eq(ShoppingCart::getDishId,dishId);
            lambdaQueryWrapper.eq(ShoppingCart::getUserId,BaseContext.getCurrentId());
            ShoppingCart one=shoppingCartService.getOne(lambdaQueryWrapper);
            Integer number=one.getNumber();
            number-=1;
            one.setNumber(number);
            shoppingCartService.updateById(one);
            if (number<=0){
                shoppingCartService.remove(lambdaQueryWrapper);
            }
            return R.success(one);
        }else {
            return null;
        }
    }
}
